*************************************************************************************
* FIGURE 1 IN PAPER:
* EVOLUTION OF THE MIGRATION PRESENCE ACROSS COLOMBIAN METROPOLITAN AREAS BY YEAR, MONTH, AND 
* MIGRATION CATEGORY
*
* LAST MODIFIED: JULY, 2025
************************************************************************************

clear all 

**# DIRECTORIES

* PAPER DIRECTORY DROPBOX
global fiscal "/Users/andres/Library/CloudStorage/Dropbox/2 Papers/2023/Paper_Fiscal_Costs"

* SUBDIRECTORIES
global data "$fiscal/1 Data/12 Final Datasets"
global programs "$fiscal/2 Programs/2 Figures and Tables paper"
global results "$fiscal/3 Results"

**# LOAD DATASET AND CREATE NEW VARIABLES

* LOAD DATASET WITH VARIABLES NEEDED
use "$data/LaborEffects.dta", clear

* GENERATE VARIABLE TO IDENTIFY MONTHS IN EACH YEAR
gen mdate = mofd(mdy(month, 1, year)) 
gen mdate2 = mdate
format mdate %tm

* GENERATE A VARIABLE TO PLOT THE MONTHS WERE THE BORDER BETWEEN THE COUNTRIES WAS CLOSED
gen borderclosed = .
replace borderclosed = 490 if mdate2 >= 667 & mdate2 < 679 /* BORDER WAS CLOSED BETWEEN AUG2015-JULY2016*/

* KEEP ONLY WORKING-AGE POPULATION
drop if (age<15|age>64) 

* RECODE IMMIGRATION VARIABLE 
recode immig (0 = 1 "Natives") ///
(1/4 = 2 "Immigrants from Venz") ///
(5 = 3 "Other")  ///
, gen(new_immig) label(new_immig)  
tab  new_immig
tab  new_immig [iw = fex12]

* RECODE IMMIGRATION VARIABLE 2
recode immig (0 = 1 "Natives") ///
(1 = 2 "Returnees") ///
(2/4 = 3 "Venz Immigrants") ///
(5 = 4 "Other")  ///
, gen(new_immig2) label(new_immig2)  
tab  new_immig2
tab  new_immig2 [iw = fex12]

**# COLLAPSE DATASET BY MONTH, YEAR, AND MIGRATION CATEGORY AND ADJUST WEIGHTS ACCORDINGLY FOR THE 23 MSAs

* ALL MIGRANTS FROM VENEZUELA
preserve
collapse (count) sex (first) month mdate2 [iw = fex12 * 12]  if area != . , by(year mdate new_immig)
keep if new_immig == 2 /*keep all the inflows from Venezuela (Venz + repatriates*/
replace  new_immig = 7 if  new_immig == 2
tempfile allMigVenz
save `allMigVenz'
restore 

* OTHER CATEGORIES
collapse (count) sex (first) month mdate2 [iw = fex12 * 12]  if area != . , by(year mdate new_immig2) 
rename new_immig2 new_immig
append using `allMigVenz'
drop if new_immig == 1
replace new_immig = 5 if new_immig == 7 /*new label for all migrants from Venz*/
sort year month new_immig
drop if  year == 2013 & month <= 3
rename sex poblacion
replace poblacion = poblacion/1000 /*rescale population to be in thousands*/

* COMMANDS TO GENERATE MOVING AVERAGES: 1) "tsegen":  2)  "tssmooth ma" 
* DECLARE DATA TO BE TIME SERIES DATA TO FACILITATES MOVING AVERAGE CALCULATIONS
tsset new_immig mdate, 

* GENERATE THE 12-MONTH MA (INCLUDING CURRENT VALUE) 
tssmooth ma ma12Poblacion =  poblacion, window(11 1 0) 

* CALCULATE GROWTH RATES IN PERCENTAGES AND LEVELS
sort new_immig  year month 

* PERCENTAGES
by new_immig (year): gen growth_ma12Poblacion  = ((ma12Poblacion - ma12Poblacion[_n-1]) / ma12Poblacion[_n-1]) * 100
by new_immig (year): gen growth_Poblacion = ((poblacion - poblacion[_n-1]) / poblacion[_n-1]) * 100

* LEVELS
by new_immig  (year): gen net_ma12Poblacion = (ma12Poblacion - ma12Poblacion[_n-1]) 
by new_immig  (year): gen net_Poblacion = (poblacion - poblacion[_n-1]) 

* GENERATE A VARIABLE TO PLOT THE MONTHS WERE THE BORDER BETWEEN THE COUNTRIES WAS CLOSED
gen borderclosed = .
replace borderclosed = 490 if mdate2 >= 667 & mdate2 < 679 /* BORDER WAS CLOSED BETWEEN AUG2015-JULY2016*/

* DROP YEAR 2020 
drop if mdate > 710

**#  FIGURE 1: EVOLUTION OF THE MONTHLY IMMIGRANT STOCK FROM VENEZUELA, 
* VENEZUELANS, REPATRIATES, AND OTHER COUNTRIES 
twoway line poblacion mdate if  new_immig  == 2 , lcolor(orange)  ///
|| line poblacion mdate if  new_immig  == 3 , lcolor(green)  ///
|| line poblacion mdate if  new_immig  == 4 , lcolor(red) ///
|| line poblacion mdate if  new_immig  == 5 , lcolor(gray)  ///
|| line  borderclosed mdate if  new_immig  == 5, lcolor(navy) lpattern(shortdash) lwidth(thick) ///
, scheme(sj) ytitle("Immigrants in thousands") xtitle("") ///
ylabel(0(500)1250) ///
xlabel(#23, labsize(vsmall) angle(45)) /// 
legend(region(lstyle(none)) label(1 "Returnees") label(2 "Venezuelan-born") label(3 "Other immigrants") label(4 "Immigrants from Venezuela") label(5 "Border closure: Aug 2015 - Jul 2016") rowgap(0) colgap(0) size(small))   ///
xline(638, lcolor(ltblue*0.5) lpattern(shortdash) noextend) ///
xline(649, lcolor(ltblue*0.5) lpattern(shortdash) noextend) ///
xline(659, lcolor(ltblue*0.5) lpattern(shortdash) noextend) ///
xline(671, lcolor(ltblue*0.5) lpattern(shortdash) noextend) ///
xline(690, lcolor(ltblue*0.5) lpattern(shortdash) noextend) ///
xline(700, lcolor(ltblue*0.5) lpattern(shortdash) noextend) ///
xline(679, lcolor(blue*0.8) lpattern(shortdash) noextend) ///
ttext(1100 638 "Maduro" "wins" "elections", place(n) box size(tiny) width(6) margin(vsmall)) /// 
ttext(1100 649 "Violent" "protests" "starts", place(n) box size(tiny) width(6) margin(vsmall)) /// 
ttext(1100 659 "Recession" "officially" "begins", place(n) box size(tiny) width(6) margin(vsmall)) /// 
ttext(1100 671 "Opposition" "wins" "supermajority," "court blocked", place(n) box size(tiny) width(8) margin(vsmall)) /// 
ttext(1100 690 "Constituent" "Assembly" "formed", place(n) box size(tiny) width(8) margin(vsmall)) /// 
ttext(1100 700 "Maduro" "Reelected", place(n) box size(tiny) width(8) margin(vsmall)) /// 
ttext(1100 679 "Border" "Reopened" "Aug 2016", place(n) box size(tiny) width(8) margin(vsmall)) /// 
graphregion(color(white)) 
* SAVE GRAPH
qui graph save "$results/1 Figures Paper/Fig1.gph", replace 
qui graph export "$results/1 Figures Paper/Fig1.eps", as(eps) replace


